Java实现批量下载图片,打包成zip压缩包

/**
 * 批量下载图片后台逻辑
 * @return
 */
@GetMapping("/downLoad")
public void download(HttpServletRequest request, HttpServletResponse response){

	try {
		String downloadFilename = System.currentTimeMillis()+".zip";//文件的名称
		downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
		response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
		response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
		ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
//            String[] files = new String[]{
//                    "https://ts1.cn.mm.bing.net/th/id/R-C.4a3983757438d048839e2e9afbf1e671?rik=hytT2hgkHaI0dQ&riu=http%3a%2f%2fb.zol-img.com.cn%2fdesk%2fbizhi%2fimage%2f3%2f960x600%2f1367830547991.jpg&ehk=z%2fcn0lolZi9CRbUsat%2b28jS48YwiwwB9exHy3ZmOpjI%3d&risl=&pid=ImgRaw&r=0",
//                    "https://tse1-mm.cn.bing.net/th/id/OIP-C.TMSwVny5FOF-5injQCrirQHaEo?w=308&h=191&c=7&r=0&o=5&pid=1.7"};
		String[] files = "https://static.bokao2o.com/c.png,https://static.bokao2o.com/b.png,https://static.bokao2o.com/a.png".split(",");
		for (int i=0;i<files.length;i++) {
			URL url = new URL(files[i]);
			zos.putNextEntry(new ZipEntry(i+".jpg"));
			InputStream fis = url.openConnection().getInputStream();
			byte[] buffer = new byte[1024];
			int r = 0;
			while ((r = fis.read(buffer)) != -1) {
				zos.write(buffer, 0, r);
			}
			fis.close();
		}
		zos.flush();
		zos.close();
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}
/**
 * 批量下载图片后台逻辑
 * @return
 */
@RequestMapping("/downLoad")
public void downLoad(HttpServletResponse response) throws IOException {
	List<String> files = Arrays.asList("https://ts1.cn.mm.bing.net/th/id/R-C.4a3983757438d048839e2e9afbf1e671?rik=hytT2hgkHaI0dQ&riu=http%3a%2f%2fb.zol-img.com.cn%2fdesk%2fbizhi%2fimage%2f3%2f960x600%2f1367830547991.jpg&ehk=z%2fcn0lolZi9CRbUsat%2b28jS48YwiwwB9exHy3ZmOpjI%3d&risl=&pid=ImgRaw&r=0",
			"https://tse1-mm.cn.bing.net/th/id/OIP-C.TMSwVny5FOF-5injQCrirQHaEo?w=308&h=191&c=7&r=0&o=5&pid=1.7");  //图片地址集合
	String downloadFilename = System.currentTimeMillis()+".zip";//文件的名称
	downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
	response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
	response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
	ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
	int i = 0;
	for (String string : files) {
		URL url = new URL(string);
		HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();
		String message = urlcon.getHeaderField(0);
		if(StringUtils.isNotEmpty(message)){
			if (message.startsWith("HTTP/1.1 404")) {
				continue;
			}
			zos.putNextEntry(new ZipEntry((++i)+".jpg"));
			InputStream fis = urlcon.getInputStream();
			byte[] buffer = new byte[1024];
			int r = 0;
			while ((r = fis.read(buffer)) != -1) {
				zos.write(buffer, 0, r);
			}
			fis.close();
		}

	}

	zos.flush();
	zos.close();


}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值